package com.jincp.filepicker.ui.file;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.R;
import com.jincp.filepicker.adapter.NormalFilePickAdapter;
import com.jincp.filepicker.base.FileBaseActivity;
import com.jincp.filepicker.bean.Directory;
import com.jincp.filepicker.bean.NormalFile;
import com.jincp.filepicker.loader.FileLoaderCallbacks;
import com.jincp.filepicker.loader.SimpleDataLoader;
import com.jincp.filepicker.widgets.PickDirectoryDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * =========================================================
 *
 * @ProjectName:
 * @ClassName: NormalFilePickActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/25 14:16
 * =========================================================
 */
public class NormalFilePickActivity extends FileBaseActivity implements Observer {

    public static final String[] SUFFIX = new String[]{"xlsx", "xls", "doc", "docX", "ppt", ".pptx", "pdf", "txt", "chm", "zip", "rar"};
    private NormalFilePickAdapter adapter;
    private Button btnCompleted;
    private Button btnDirectory;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_normal_file_pick;
    }

    @Override
    protected void initViews() {
        setCustomToolBar((Toolbar) findViewById(R.id.toolbar), new ToolBarOptions());
        configRecyclerView();
        btnDirectory = findViewById(R.id.btn_directory);
        btnDirectory.setText("全部文件");
        btnDirectory.setOnClickListener(this);

        btnCompleted = findViewById(R.id.btn_complete);
        btnCompleted.setVisibility(NormalFilePickerManager.getInstance().isMultiMode() ? View.VISIBLE : View.GONE);
        btnCompleted.setText(R.string.complete);
        btnCompleted.setOnClickListener(this);
    }

    private void configRecyclerView() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new MyLinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new NormalFilePickAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NormalFile normalFile = (NormalFile) adapter.getItem(position);
                if (NormalFilePickerManager.getInstance().isMultiMode()) {
                    NormalFilePickerManager.getInstance().previewFile(NormalFilePickActivity.this, normalFile);
                } else {
                    NormalFilePickerManager.getInstance().addSelectedFile(normalFile);
                    NormalFilePickerManager.getInstance().completedSelector();
                    finish();
                }

            }
        });
        adapter.bindToRecyclerView(recyclerView);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        NormalFilePickerManager.getInstance().addObserver(this);
        showWaitDialog(getString(R.string.loading));
        SimpleDataLoader.getFiles(NormalFilePickActivity.this, new FileLoaderCallbacks.FileLoadResultCallback<NormalFile>() {
            @Override
            public void onResult(List<Directory<NormalFile>> directoryList) {
                hideWaitDialog();
                if (directoryList == null || directoryList.isEmpty()) {
                    adapter.setEmptyView(R.layout.empty_view);
                    return;
                }
                NormalFilePickerManager.getInstance().setDirectoryList(directoryList);
                Directory firstDirectory = directoryList.get(0);
                firstDirectory.setSelected(true);
                adapter.setNewData(firstDirectory.getFiles());
            }
        }, SUFFIX);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_complete) {
            NormalFilePickerManager.getInstance().completedSelector();
        } else if (view.getId() == R.id.btn_directory) {
            if (NormalFilePickerManager.getInstance().getDirectoryList() == null) {
                ToastUtil.getInstance().show(R.string.empty_no_data);
                return;
            }
            PickDirectoryDialog dialog = new PickDirectoryDialog.Builder()
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setGravity(Gravity.BOTTOM)
                    .setDirectoryList(NormalFilePickerManager.getInstance().getDirectoryList())
                    .setDirectoryType(Constant.TYPE_FILE)
                    .build();
            dialog.setDialogResultListener(new DialogResultListener() {
                @Override
                public void result(DialogFragment dialog, Object result) {
                    dialog.dismissAllowingStateLoss();
                    int position = (int) result;
                    NormalFilePickerManager.getInstance().setCurrentDirectoryIndex(position);
                    adapter.setNewData(NormalFilePickerManager.getInstance().getCurrentDirectoryFileList());
                }
            });
            dialog.setDialogDismissListener(new DialogDismissListener() {
                @Override
                public void dismiss(DialogFragment dialog) {
                    btnDirectory.setEnabled(true);
                }
            });
            dialog.show(getSupportFragmentManager(), "PickDirectoryDialog");
            btnDirectory.setEnabled(false);
        }
    }

    @Override
    public void update(Observable observable, Object arg) {
        if (observable instanceof NormalFilePickerManager) {
            if (NormalFilePickerManager.getInstance().getSelectFileCount() > 0) {
                btnCompleted.setText(getString(R.string.selected_complete, NormalFilePickerManager.getInstance().getSelectFileCount(), NormalFilePickerManager.getInstance().getMaxSelectedLimit()));
                btnCompleted.setEnabled(true);
            } else {
                btnCompleted.setText(getString(R.string.complete));
                btnCompleted.setEnabled(false);
            }
        }
    }
}
